/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SignupView.java
 *
 * Created on 20-12-2011, 10:48:43
 */
package view;

import controller.NoSuchGuestException;
import controller.SignupController;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import model.Activity;
import model.Signup;

/**
 *
 * @author Mads Nielsen <k4emic@gmail.com>
 */
public class SignupView extends javax.swing.JFrame {
    
    private SignupController controller;
    private Activity activity;

    /** Creates new form SignupView */
    public SignupView() {
        initComponents();
        
        setLocationRelativeTo(null);
        
        controller = new SignupController();
        setResizable(false);
    }
    
    /**
     * Starts signup process
     * @param selectedActivity 
     */
    public void startSignup(Activity selectedActivity) {
        activity = selectedActivity;
        lblActivityName.setText(activity.getName());
    }
    
    private void showConfirmSignupDialog(Signup signup) {
        
        lblConfirmActivityName.setText(signup.getActivity().getName());
        lblConfirmGuestName.setText(signup.getGuest().getName());
        lblConfirmPlace.setText(signup.getActivity().getRoom().getName());
        
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE H:mm");
        String timeString = sdf.format(signup.getActivity().getTimeOfStart());
        lblConfirmTime.setText(timeString);        
        
        diaConfirmSignup.setLocationRelativeTo(this);
        diaConfirmSignup.pack();
        diaConfirmSignup.setVisible(true);
    }
    
    /**
     * Called when the guest has been successfully registered
     * @param cpr 
     */
    public void onAfterRegister(String cpr) {
        
        try {
            controller.createSignup(activity, cpr);
        } catch(NoSuchGuestException e) {
            
        }
        
        showConfirmSignupDialog(controller.getPendingSignup());
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        diaConfirmSignup = new javax.swing.JDialog();
        btnConfirmSignup = new javax.swing.JButton();
        btnCancelSignup = new javax.swing.JButton();
        lblConfirmGuestName = new javax.swing.JLabel();
        lblConfirmActivityName = new javax.swing.JLabel();
        lblConfirmTime = new javax.swing.JLabel();
        lblConfirmPlace = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCpr = new javax.swing.JPasswordField();
        btnConfirm = new javax.swing.JButton();
        lblActivityName = new javax.swing.JLabel();

        diaConfirmSignup.setName("diaConfirmSignup"); // NOI18N

        btnConfirmSignup.setText("Tilmeld");
        btnConfirmSignup.setName("btnConfirmSignup"); // NOI18N
        btnConfirmSignup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmSignupActionPerformed(evt);
            }
        });

        btnCancelSignup.setText("Afbryd");
        btnCancelSignup.setName("btnCancelSignup"); // NOI18N
        btnCancelSignup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelSignupActionPerformed(evt);
            }
        });

        lblConfirmGuestName.setText("guest name");
        lblConfirmGuestName.setName("lblConfirmGuestName"); // NOI18N

        lblConfirmActivityName.setText("activity name");
        lblConfirmActivityName.setName("lblConfirmActivityName"); // NOI18N

        lblConfirmTime.setText("time");
        lblConfirmTime.setName("lblConfirmTime"); // NOI18N

        lblConfirmPlace.setText("place");
        lblConfirmPlace.setName("lblConfirmPlace"); // NOI18N

        jLabel3.setText("-");
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout diaConfirmSignupLayout = new javax.swing.GroupLayout(diaConfirmSignup.getContentPane());
        diaConfirmSignup.getContentPane().setLayout(diaConfirmSignupLayout);
        diaConfirmSignupLayout.setHorizontalGroup(
            diaConfirmSignupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diaConfirmSignupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(diaConfirmSignupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblConfirmActivityName)
                    .addGroup(diaConfirmSignupLayout.createSequentialGroup()
                        .addComponent(lblConfirmTime)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblConfirmPlace))
                    .addComponent(lblConfirmGuestName)
                    .addGroup(diaConfirmSignupLayout.createSequentialGroup()
                        .addComponent(btnConfirmSignup)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelSignup)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        diaConfirmSignupLayout.setVerticalGroup(
            diaConfirmSignupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diaConfirmSignupLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblConfirmActivityName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(diaConfirmSignupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblConfirmTime)
                    .addComponent(jLabel3)
                    .addComponent(lblConfirmPlace))
                .addGap(18, 18, 18)
                .addComponent(lblConfirmGuestName)
                .addGap(18, 18, 18)
                .addGroup(diaConfirmSignupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelSignup)
                    .addComponent(btnConfirmSignup))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });

        jPanel2.setName("jPanel2"); // NOI18N

        jLabel2.setText("Tilmelding til");
        jLabel2.setName("jLabel2"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Indtast dit personnummer (CPR)");
        jLabel1.setName("jLabel1"); // NOI18N

        txtCpr.setFont(new java.awt.Font("Tahoma", 0, 18));
        txtCpr.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        txtCpr.setName("txtCpr"); // NOI18N

        btnConfirm.setText("Tilmeld");
        btnConfirm.setName("btnConfirm"); // NOI18N
        btnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCpr, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnConfirm, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnConfirm, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtCpr))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblActivityName.setText("aktivitetnavn");
        lblActivityName.setName("lblActivityName"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblActivityName)
                .addContainerGap(113, Short.MAX_VALUE))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblActivityName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmActionPerformed

        // test if the activity has reached its signup limit
        
        // get typed cpr
        String cpr = new String(txtCpr.getPassword());
        
        // test if cpr is valid
        Pattern pattern = Pattern.compile("^[0-9]{6}-?[0-9]{4}$");
        Matcher matcher = pattern.matcher(cpr);
        
        if(!matcher.matches()) {
            JOptionPane.showMessageDialog(this, "Du indtastede ikke et gyldigt personnummer. Prøv igen.", "Fejl", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        try {
            //create signup
            controller.createSignup(activity, cpr);
            
            showConfirmSignupDialog(controller.getPendingSignup());
        } catch(NoSuchGuestException e) {
            // start guest registration
            GuestView guestv = new GuestView();
            guestv.startRegistration(cpr, this);
            guestv.setLocationRelativeTo(this);
            guestv.setVisible(true);
        }
        
        dispose();
        
    }//GEN-LAST:event_btnConfirmActionPerformed

    private void btnConfirmSignupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmSignupActionPerformed
        controller.confirmSignup();        
        JOptionPane.showMessageDialog(diaConfirmSignup, "Du er nu tilmeldt");
        diaConfirmSignup.dispose();
    }//GEN-LAST:event_btnConfirmSignupActionPerformed

    private void btnCancelSignupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelSignupActionPerformed
        diaConfirmSignup.dispose();
        dispose();
    }//GEN-LAST:event_btnCancelSignupActionPerformed

    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowLostFocus
        dispose();
    }//GEN-LAST:event_formWindowLostFocus

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelSignup;
    private javax.swing.JButton btnConfirm;
    private javax.swing.JButton btnConfirmSignup;
    private javax.swing.JDialog diaConfirmSignup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblActivityName;
    private javax.swing.JLabel lblConfirmActivityName;
    private javax.swing.JLabel lblConfirmGuestName;
    private javax.swing.JLabel lblConfirmPlace;
    private javax.swing.JLabel lblConfirmTime;
    private javax.swing.JPasswordField txtCpr;
    // End of variables declaration//GEN-END:variables

}
